package cn.su.servlet;

import cn.su.pojo.User;
import cn.su.pojo.fenye;
import cn.su.pojo.role;
import cn.su.service.UserService;
import cn.su.service.impl.Userserviceimpl;
import cn.su.utils.BaseDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "UserServlet", value = "/jsp/user.do")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String method = request.getParameter("method");//用来判断执行什么功能
        HttpSession session = request.getSession();
        System.out.println("用户User:"+request.getSession().getAttribute("user"));
        if (method.equals("query")){
            UserService userService=new Userserviceimpl();
            //把角色放入session
            List<role> roles = userService.role_list();
            session.setAttribute("roleList",roles);  //这里role放在session，这样在修改也可以看到

            /*---------------分页----------------*/
            int zcount = userService.User_count();

            fenye fenye=new fenye();
            fenye.totalCount=zcount;
            if (request.getParameter("pageIndex")==null||request.getParameter("pageIndex").equals("")) {
                request.setAttribute("currentPageNo",1);
                fenye.currentPageNo=1;
            }else {
                fenye.currentPageNo=Integer.parseInt(request.getParameter("pageIndex"));

            }

            if (zcount%12==0){
                fenye.totalPageCount =zcount/12;
            }else{
                fenye.totalPageCount  =zcount/12+1;
            }

            request.setAttribute("totalPageCount", fenye.totalPageCount);
            request.setAttribute("totalCount",zcount);
            request.setAttribute("currentPageNo",request.getParameter("pageIndex"));
            /*------------------------------*/

            List<User> users = userService.User_list(fenye);

            request.setAttribute("userList",users);
            //查看是否有查询
            String queryname = request.getParameter("queryname");
            //queryUserRole得到就是role的id
            String queryUserRole = request.getParameter("queryUserRole");
            if (queryname!=null&&queryname.equals("") && queryUserRole!=null&&!queryUserRole.equals("0")){
             request.setAttribute("queryname",queryname);
             request.setAttribute("queryUserRole",queryUserRole);
                List<User> users1 = userService.USer_search(queryname, queryUserRole);
                request.setAttribute("userList",users1);

            }

    request.getRequestDispatcher("userlist.jsp").forward(request, response);

        }
          if (method.equals("add")){ //添加用户
              String userCode = request.getParameter("userCode");
              String userName = request.getParameter("userName");
              String userPassword = request.getParameter("userPassword");//密码
              String ruserPassword = request.getParameter("ruserPassword");//确认密码
              String gender = request.getParameter("gender");
              String birthday = request.getParameter("birthday");
              String phone = request.getParameter("phone");
              String address = request.getParameter("address");
              String userRole = request.getParameter("userRole");
              if (ruserPassword==null||ruserPassword.equals("")||!userPassword.equals(ruserPassword)){
                  request.setAttribute("error","密码不一致");
                  request.getRequestDispatcher("useradd.jsp").forward(request, response);
              }else if (userPassword.equals(ruserPassword)){
                  User user=new User();
                  user.setUserCode(userCode);
                  user.setUserName(userName); user.setUserPassword(userPassword); user.setGender(Integer.parseInt(gender));
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                  try {
                      user.setBirthday(sdf.parse(birthday)); //把字符串转成日期
                  } catch (ParseException e) {
                      e.printStackTrace();
                  }
                  user.setPhone(phone);
                  user.setAddress(address);user.setUserRole(Integer.parseInt(userRole));
                  user.setCreationDate(new Date()); //创建日期
                  User user1 = (User) request.getSession().getAttribute("user");//找到创建者
                  user.setCreatedBy(user1.getId());
                  UserService servlet=new Userserviceimpl();
                  int i = servlet.User_add(user);
                  if (i>0){
                      request.setAttribute("error","添加成功");
                      request.getRequestDispatcher("useradd.jsp").forward(request, response);
                  }else{
                      request.getRequestDispatcher("error.jsp").forward(request, response);
                  }


              }else{
                  request.getRequestDispatcher("error.jsp").forward(request, response);
              }


          }
          if (method.equals("chakan")){
              String id = request.getParameter("id");
              int id1=Integer.parseInt(id);
              UserService userService=new Userserviceimpl();
              User user = userService.User_chakan(id1);
              request.setAttribute("user",user);
              request.getRequestDispatcher("userview.jsp").forward(request, response);
          }


          if (method.equals("xiugai")){
              String id = request.getParameter("id");
              int id1=Integer.parseInt(id);
              UserService userService=new Userserviceimpl();
              User user = userService.User_chakan(id1);
              request.setAttribute("user",user);
              request.getRequestDispatcher("usermodify.jsp").forward(request, response);
          }
          if (method.equals("modifyexe")){ //修改保存
              String userName = request.getParameter("userName");
              String gender = request.getParameter("gender");
              String birthday = request.getParameter("birthday");
              String phone = request.getParameter("phone");
              String address = request.getParameter("address");
              String userRole = request.getParameter("userRole");//这里得到的是用户职能，还不职能表的id
              User user=new User();
              user.setUserName(userName);
              user.setGender(Integer.parseInt(gender));
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
              try {
                  user.setBirthday(sdf.parse(birthday)); //把字符串转成日期
              } catch (ParseException e) {
                  e.printStackTrace();
              }

              user.setPhone(phone);
              user.setAddress(address);
              user.setRoleName(userRole);//这里是职能，一会在数据库的时候查找该职能表的id
              user.setModifyDate(new Date()); //修改日期
              User user1 = (User) request.getSession().getAttribute("user");//找到修改者
              user.setModifyBy(user1.getId());
            UserService userService=new Userserviceimpl();
              String uid = request.getParameter("uid");//用户id
              int i = userService.User_update(Integer.parseInt(uid), user);
              if (i>0){//修改成功

                  request.getRequestDispatcher("/jsp/user.do?method=query").forward(request, response);

              }else {
                  request.getRequestDispatcher("error.jsp").forward(request, response);
              }


          }

          if (method.equals("delete")){
              String id = request.getParameter("id");
              UserService userService=new Userserviceimpl();
              int i = userService.User_delete(Integer.parseInt(id));
              if (i>0){
                  request.getRequestDispatcher("/jsp/user.do?method=query").forward(request, response);
              }else{
                  request.getRequestDispatcher("error.jsp").forward(request, response);
              }
          }


        if (method.equals("savepwd")){//修改密码
            String oldpassword = request.getParameter("oldpassword");
            String newpassword = request.getParameter("newpassword");
            String rnewpassword = request.getParameter("rnewpassword");
            System.out.println(oldpassword+"==="+newpassword+"==="+rnewpassword);
            if (!newpassword.equals(rnewpassword)){
                request.setAttribute("oldpassword",oldpassword);
                request.setAttribute("newpassword",newpassword);
                request.setAttribute("rnewpassword",rnewpassword);
                request.setAttribute("message","两次密码输入不一致");
                request.getRequestDispatcher("pwdmodify.jsp").forward(request,response);
            }else{
                   UserService servlet=new Userserviceimpl();
                int xiugai = servlet.xiugai(oldpassword, newpassword);
                if (xiugai>0){
                    response.getWriter().write("<script type='text/javascript'>alert('修改成功')</script>");
                    request.setAttribute("message","修改成功");
                    request.getRequestDispatcher("pwdmodify.jsp").forward(request, response);
                }else{
                    request.setAttribute("message","原密码错误");
                    request.getRequestDispatcher("pwdmodify.jsp").forward(request, response);
                }
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       this.doGet(request, response);
    }
}
